Wordpress even odd post count

35

<?php
    $count = 0;
    if (have_posts()): while (have_posts()) : the_post();
        if ( ++$count == $wp_query->post_count && ( $wp_query->post_count % 2 ) == 1 ) {
            // if final count is reached AND final count is odd
            // full width item
            $postclass = "fullwidth";
            $opentag = '';
            $closingtag = '</div>';
        } else if ( ( $count % 2 ) == 1 ) {
            // if $count is odd it is the first item in a 'row'
            $postclass = "halfwidth first";
            $opentag = '<div class="home-ci-row">';
            $closingtag = '';
        } else {
            // second item in a row
            $postclass = "halfwidth second";
            $opentag = '';
            $closingtag = '</div>';
        }
?>
    <?php echo $opentag; ?>
    <div class="main-column-item-wrap <?php echo $postclass; ?>">
    CONTENT OF POST : Title, Thumbnail, Excerpt... etc
    </div><!-- main-column-item-wrap -->
    <?php echo $closingtag; ?>

<?php endwhile; endif; ?>

Comments

Submit
0 Comments